package com.app.service;

import com.app.model.main.Agreement;
import com.app.model.main.Feedback;
import com.app.model.main.Image;
import com.app.model.main.Version;
import com.app.model.response.AgreementResponseBody;
import com.app.model.response.RetCode;
import com.app.model.response.RetStatus;
import com.app.model.response.VersionResponseBody;
import com.app.repository.interfaces.AgreementMapper;
import com.app.repository.interfaces.FeedbackMapper;
import com.app.repository.interfaces.ImageMapper;
import com.app.repository.interfaces.VersionMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Lichaojie on 2017/8/11. It's a beautiful day.
 */
@Service
public class CommonService {

    @Autowired
    private VersionMapper versionMapper;
    @Autowired
    private AgreementMapper agreementMapper;
    @Autowired
    private ImageMapper imageMapper;
    @Autowired
    private ResponseService responseService;
    @Autowired
    private FeedbackMapper feedbackMapper;


    public RetStatus getVersionInfo(){
        Version version = versionMapper.selectByPrimaryKey(versionMapper.getMaxId());

        VersionResponseBody versionResponseBody = new VersionResponseBody();
        versionResponseBody.setVersionInfo(version.getVersionInfo())
                .setVersionNum(version.getVersionNum());

        RetStatus retStatus = new RetStatus();
        retStatus.setCode(100)
                .setMessage(RetCode.getMessage(100))
                .setBody(versionResponseBody);

        return retStatus;
    }

    public RetStatus getAgreement(){
        Agreement agreement = agreementMapper.selectByPrimaryKey(agreementMapper.getMaxId());

        AgreementResponseBody agreementResponseBody = new AgreementResponseBody();
        agreementResponseBody.setAgreement(agreement.getAgreementInfo())
                .setAgreementNum(agreement.getAgreementNum())
                .setVersionNum(agreement.getVersionNum());

        RetStatus retStatus = new RetStatus();
        retStatus.setCode(100)
                .setMessage(RetCode.getMessage(100))
                .setBody(agreementResponseBody);

        return retStatus;
    }

    public RetStatus feedback(String parameter){
        ObjectMapper objectMapper = new ObjectMapper();
        Map paraMap = null;
        try{
            paraMap = objectMapper.readValue(parameter,Map.class);
        }catch (IOException e){
            e.printStackTrace();
            return responseService.doResponse(204);
        }

        int id = (int)paraMap.get("userId");
        String text = (String)paraMap.get("text");
        String time = (String)paraMap.get("time");
        List<String> urls = (List<String>)paraMap.get("urls");
        if(text == null){
            text = "";
        }
        if(urls == null){
            urls = new ArrayList<>();
        }

        List<Long> ids = new ArrayList<>();
        for (String str : urls){
            long urlId;
            Image image = new Image();
            image.setOrigin(str);
            Image image1;
            image1 = imageMapper.selectByImage(str);
            if(image1 == null){
                if(imageMapper.insertSelective(image) == 1){
                    image1 = imageMapper.selectByImage(str);
                }else {
                    return responseService.doResponse(200);
                }
            }
            urlId = image1.getId();
            ids.add(urlId);
        }

        String url = "";
        for(Long i : ids){
            url += (i + ",");
        }
        url = url.substring(0,url.length() - 1);

        Feedback feedback = new Feedback();
        feedback.setUserId((long)id)
                .setContent(text)
                .setImage(url)
                .setTime(time)
                .setIsTackled((byte) 0);

        if(feedbackMapper.insertSelective(feedback) == 1){
            return responseService.doResponse(100);
        }else {
            return responseService.doResponse(200);
        }
    }
}
